package com.assignwork.modules.secondarydispatch;

import com.assignwork.dto.AssignWorkDTO;
import com.assignwork.dto.DailyPlanDTO;
import com.assignwork.dto.WorkerInfoDTO;
import com.assignwork.modules.secondarydispatch.service.ISecondaryDispatchService;
import com.assignwork.service.*;
import lombok.AllArgsConstructor;
import org.springblade.common.cache.CacheNames;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("secondary-dispatch")
public class SecondaryDispatchController {

    ISecondaryDispatchService dispatchService;
    IWorkArrangeShipService arrangeShipService;
    IWorkArrangeLandService arrangeLandService;
    IWorkerInfoService workerInfoService;
    IAssignWorkService assignWorkService;
    IWorkArrangeOtherService arrangeOtherService;
    IDailyPlanService dailyPlanService;
    IWorkLineLogService workLineLogService;

    @GetMapping("list-daily-plan")
    public R listDailyPlan(BladeUser bladeUser, DailyPlanDTO dto) {
        return R.data(dailyPlanService.listDailyPlan4sd(bladeUser, dto));
    }

    @GetMapping("list-work-arrange-ship")
    public R listWorkArrangeShip(BladeUser bladeUser, AssignWorkDTO dto) {
        return R.data(arrangeShipService.listWorkArrangeShip(bladeUser, dto));
    }

    @GetMapping("page-worker-info")
    public R pageWorkerInfo(BladeUser bladeUser, Query query, WorkerInfoDTO dto) {
        return R.data(workerInfoService.pageWorkerInfo(bladeUser, Condition.getPage(query), dto));
    }

    @GetMapping("list-worker-info")
    @Cacheable(value = CacheNames.ALL_WORKER_INFO, key = "#dto.groupKey")
    public R listWorkerInfo(BladeUser bladeUser, WorkerInfoDTO dto) {
        return R.data(workerInfoService.listWorkerInfo(bladeUser, dto));
    }

    @PostMapping("save-or-update-worker-info")
    @CacheEvict(value = CacheNames.ALL_WORKER_INFO, allEntries = true)
    public R saveOrUpdateWorkerInfo(BladeUser bladeUser, @RequestBody WorkerInfoDTO dto) {
        return R.data(workerInfoService.saveOrUpdate(dto));
    }

    @PostMapping("remove-worker-info")
    @CacheEvict(value = CacheNames.ALL_WORKER_INFO, allEntries = true)
    public R removeWorkerInfo(BladeUser bladeUser, @RequestParam String id) {
        return R.data(workerInfoService.removeById(Func.toLong(id)));
    }

    @PostMapping("save-or-update-assign-work")
    public R saveOrUpdateAssignWork(BladeUser bladeUser, @RequestBody List<AssignWorkDTO> dtos) {
        return R.data(this.assignWorkService.saveOrUpdateAssignWork(bladeUser, dtos));
    }

    @GetMapping("list-assign-work")
    public R listAssignWork(BladeUser bladeUser, AssignWorkDTO dto) {
        return R.data(this.assignWorkService.listAssignWork(bladeUser, dto));
    }

    @GetMapping("page-work-arrange-land")
    public R pageWorkArrangeLand(BladeUser bladeUser, Query query, AssignWorkDTO dto) {
        return R.data(this.arrangeLandService.pageWorkArrangeLand(bladeUser, Condition.getPage(query), dto));
    }

    @GetMapping("page-work-arrange-other")
    public R pageWorkArrangeOther(BladeUser bladeUser, Query query, AssignWorkDTO dto) {
        return R.data(this.arrangeOtherService.pageWorkArrangeOther(bladeUser, Condition.getPage(query), dto));
    }

    @GetMapping("work-line-log-by-arrange-id")
    public R workLineLogByArrangeId(String arrangeId, String workType) {
        return R.data(workLineLogService.workLineLogByArrangeId(Func.toLong(arrangeId), workType));
    }

    @GetMapping("arrange-info-by-arrangeId")
    public R arrangeInfoByArrangeId(String arrangeId) {
        return R.data(arrangeShipService.arrangeInfoByArrangeId(Func.toLong(arrangeId)));
    }

    @GetMapping("arrange-info-land-by-arrangeId")
    public R arrangeInfoLandByArrangeId(String arrangeId) {
        return R.data(arrangeLandService.arrangeInfoLandByArrangeId(Func.toLong(arrangeId)));
    }
}
